using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BerkoRules.Objects;
using Microsoft.VisualBasic.CompilerServices;
using BerkoRules.Mappers;
using System.IO;
using BerkoRules.Entities;
using Microsoft.VisualBasic;
using System.Configuration;

namespace Berko
{
    public partial class frmContactos : Form
    {
        private ContactosList _contactos;
        private Contactos_DireccionesList _direcciones;
        private bool _esModificacion;
        private bool _cambio;
        private int _indexContacto;
        private Contactos_PersonasObjectList _personas;


        public frmContactos()
        {
            base.Load += new EventHandler(this.frmContactos_Load);
            //__ENCList.Add(new WeakReference(this));
            this._direcciones = null;
            this._personas = null;
            this._contactos = null;
            this._indexContacto = 0;
            this._esModificacion = false;
            this._cambio = false;
            this.InitializeComponent();
        }

        private void BindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            this._indexContacto = 0;
            this.Cargar_Contacto();
        }

        private void BindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            this._indexContacto = this._contactos.Count - 1;
            this.Cargar_Contacto();
        }

        private void BindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            this._indexContacto++;
            this.Cargar_Contacto();
        }

        private void BindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            this._indexContacto--;
            this.Cargar_Contacto();
        }

        private void Cargar_Contacto()
        {
            this.Traer_Direcciones(this._contactos[this._indexContacto].con_ID);
            this.Traer_Personas(this._contactos[this._indexContacto].con_ID);
            this.txtActividad.Text = this._contactos[this._indexContacto].con_Actividad;
            if (this._contactos[this._indexContacto].con_Categoria.HasValue)
            {
                this.cboCategorias.SelectedIndex = this._contactos[this._indexContacto].con_Categoria.Value - 1;
            }
            else
            {
                this.cboCategorias.Text = string.Empty;
            }
            this.txtEmpresa.Text = this._contactos[this._indexContacto].con_Nombre;
            if (this._contactos[this._indexContacto].con_CodCont.HasValue)
            {
                this.txtCodCont.Text = this._contactos[this._indexContacto].con_CodCont.Value.ToString();
            }
            if (!File.Exists(System.Configuration.ConfigurationSettings.AppSettings["textosCon"] + this._contactos[this._indexContacto].con_ID.ToString() + ".rtf"))
            {
                try
                {
                    StreamWriter writer = new StreamWriter(System.Configuration.ConfigurationSettings.AppSettings["textosCon"] + this._contactos[this._indexContacto].con_ID.ToString() + ".rtf");
                    writer.Write(this._contactos[this._indexContacto].con_Historial);
                    writer.Close();
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    ProjectData.ClearProjectError();
                }
            }
            try
            {
                this.txtHistorial.LoadFile(System.Configuration.ConfigurationSettings.AppSettings["textosCon"] + this._contactos[this._indexContacto].con_ID.ToString() + ".rtf", RichTextBoxStreamType.RichText);
            }
            catch (Exception exception3)
            {
                ProjectData.SetProjectError(exception3);
                Exception exception2 = exception3;
                this.txtHistorial.SelectionFont = this.txtCodCont.Font;
                this.txtHistorial.SelectionColor = Color.Black;
                this.txtHistorial.Font = this.txtCodCont.Font;
                this.txtHistorial.Text = this._contactos[this._indexContacto].con_Historial;
                ProjectData.ClearProjectError();
            }
            this.txtEmail.Text = this._contactos[this._indexContacto].con_Email;
            this.txtSitio.Text = this._contactos[this._indexContacto].con_SitioWeb;
            this.chkAviso.Checked = this._contactos[this._indexContacto].con_Aviso;
        }

        private void cboActividades_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                this.txtEmail.Focus();
            }
        }

        private void cboBuscar2_Click(object sender, EventArgs e)
        {
            if (this.cboCampos.SelectedIndex > -1)
            {
                this.Traer_X_Campo();
                if (this._contactos != null)
                {
                    if (this._contactos.Count > 0)
                    {
                        this._indexContacto = 0;
                        this.Cargar_Contacto();
                        this.BindingNavigatorMoveFirstItem.PerformClick();
                        this.cmdModificar.Enabled = true;
                        this.cmdEliminar.Enabled = true;
                    }
                    else
                    {
                        this.Limpiar_Contacto();
                    }
                }
                else
                {
                    this.Limpiar_Contacto();
                }
            }
        }

        private void cboCategorias_Validated(object sender, EventArgs e)
        {
            if ((this.cboCategorias.Text.Trim() != string.Empty) && (this.cboCategorias.SelectedIndex == -1))
            {
                MessageBox.Show("El nombre de la categor\x00eda no existe", "Campo inexistente", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.cboCategorias.Text = string.Empty;
            }
        }

        private void chkItalic_CheckedChanged(object sender, EventArgs e)
        {
            if (!this._cambio)
            {
                this.Modificar_Texto();
            }
            this.txtHistorial.Focus();
        }

        private void chkNegrita_CheckedChanged(object sender, EventArgs e)
        {
            if (!this._cambio)
            {
                this.Modificar_Texto();
            }
            this.txtHistorial.Focus();
        }

        private void chkSubrayado_CheckedChanged(object sender, EventArgs e)
        {
            if (!this._cambio)
            {
                this.Modificar_Texto();
            }
            this.txtHistorial.Focus();
        }

        private void chkTachado_CheckedChanged(object sender, EventArgs e)
        {
            if (!this._cambio)
            {
                this.Modificar_Texto();
            }
            this.txtHistorial.Focus();
        }

        private void cmdBuscar3_Click(object sender, EventArgs e)
        {
            if (this.cboCategorias2.SelectedIndex > -1)
            {
                this.Traer_X_Categoria();
                if (this._contactos != null)
                {
                    if (this._contactos.Count > 0)
                    {
                        this._indexContacto = 0;
                        this.Cargar_Contacto();
                        this.BindingNavigatorMoveFirstItem.PerformClick();
                        this.cmdModificar.Enabled = true;
                        this.cmdEliminar.Enabled = true;
                    }
                    else
                    {
                        this.Limpiar_Contacto();
                    }
                }
                else
                {
                    this.Limpiar_Contacto();
                }
            }
        }

        private void cmdEliminar_Click(object sender, EventArgs e)
        {
            this.panEliminar.Visible = true;
            this.cmdNo.Focus();
            this.cmdEliminar.Enabled = false;
            this.cmdGuardar.Enabled = false;
            this.cmdNuevo.Enabled = false;
            this.cmdModificar.Enabled = false;
            this.bnNavegador.Enabled = false;
            this.Habilitar_Pantalla(false);
        }

        private void cmdFuente_Click(object sender, EventArgs e)
        {
            if (this.FontDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtHistorial.SelectionFont = this.FontDialog1.Font;
                this.txtHistorial.SelectionColor = this.FontDialog1.Color;
            }
            this.txtHistorial.Focus();
        }

        private void cmdFuente2_Click(object sender, EventArgs e)
        {
            this.txtHistorial.SelectionFont = this.txtCodCont.Font;
            this.txtHistorial.SelectionColor = Color.Black;
            this.txtHistorial.Focus();
        }

        private void cmdLimpiar_Click(object sender, EventArgs e)
        {
            this._esModificacion = false;
            this.Limpiar_Contacto();
            if (this.txtEmpresa.Enabled)
            {
                this.Habilitar_Pantalla(false);
                this.cmdGuardar.Enabled = false;
            }
            else
            {
                this.Habilitar_Pantalla(true);
                this.cmdGuardar.Enabled = true;
            }
        }

        private void cmdModificar_Click(object sender, EventArgs e)
        {
            if (this.txtEmpresa.Enabled)
            {
                this.Habilitar_Pantalla(false);
                this.cmdGuardar.Enabled = false;
                this._esModificacion = false;
            }
            else
            {
                this.Habilitar_Pantalla(true);
                this.cmdGuardar.Enabled = true;
                this._esModificacion = true;
            }
        }

        private void cmdNo_Click(object sender, EventArgs e)
        {
            this.panEliminar.Visible = false;
            this.cmdEliminar.Enabled = true;
            this.cmdGuardar.Enabled = false;
            this.cmdNuevo.Enabled = true;
            this.cmdModificar.Enabled = true;
            this.bnNavegador.Enabled = true;
        }

        private void cmdNuevo_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("\x00bfDese guardar los cambios realizados?", "Guardar", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) && this.Validar_Contacto())
            {
                this.Guardar_Contacto();
                this.cmdGuardar.Enabled = false;
                this.Habilitar_Pantalla(false);
            }
        }

        private void cmdPasarCli_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("\x00bfDesea quitar el contacto y pasarlo a la lista de clientes?", "Contacto a cliente", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) && this.Pasar_A_Cliente())
            {
                this.Eliminar_Contacto();
            }
        }

        private void cmdSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdSi_Click(object sender, EventArgs e)
        {
            this.Eliminar_Contacto();
            this.panEliminar.Visible = false;
            this.cmdEliminar.Enabled = true;
            this.cmdGuardar.Enabled = false;
            this.cmdNuevo.Enabled = true;
            this.cmdModificar.Enabled = true;
            this.bnNavegador.Enabled = true;
            this.Limpiar_Contacto();
        }

        private void cmdTodos_Click(object sender, EventArgs e)
        {
            this.Traer_Contactos();
            if ((this._contactos != null) && (this._contactos.Count > 0))
            {
                this.Cargar_Contacto();
                this.cmdModificar.Enabled = true;
                this.cmdEliminar.Enabled = true;
            }
        }

        private void Crear_Contacto()
        {
            Contactos row = new Contactos();
            row.con_Actividad = this.txtActividad.Text;
            if (this.cboCategorias.SelectedIndex > -1)
            {
                row.con_Categoria = (byte)(this.cboCategorias.SelectedIndex + 1);
            }
            if (this.txtEmail.Text.Trim() != string.Empty)
            {
                row.con_Email = this.txtEmail.Text.Trim();
            }
            if (this.txtHistorial.Text.Trim() != string.Empty)
            {
                row.con_Historial = this.txtHistorial.Text.Trim();
            }
            if ((this.txtCodCont.Text.Trim() != string.Empty) && Versioned.IsNumeric(this.txtCodCont.Text))
            {
                row.con_CodCont = Conversions.ToShort(this.txtCodCont.Text);
            }
            row.con_Nombre = this.txtEmpresa.Text.Trim();
            row.con_Aviso = this.chkAviso.Checked;
            if (this.txtSitio.Text.Trim() != string.Empty)
            {
                row.con_SitioWeb = this.txtSitio.Text.Trim();
            }
            row.con_FechaHora = DateTime.Now;
            try
            {
                ContactosMapper.Instance().Insert(row);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudo guardar el contacto. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                ProjectData.ClearProjectError();
                return;
            }
            try
            {
                StreamWriter writer = new StreamWriter(System.Configuration.ConfigurationSettings.AppSettings["textosCon"] + row.con_ID.ToString() + ".rtf");
                writer.Write(this.txtHistorial.Rtf);
                writer.Close();
                this._cambio = true;
                this.chkItalic.Checked = false;
                this.chkNegrita.Checked = false;
                this.chkSubrayado.Checked = false;
                this.chkTachado.Checked = false;
            }
            catch (Exception exception5)
            {
                ProjectData.SetProjectError(exception5);
                Exception exception2 = exception5;
                ProjectData.ClearProjectError();
            }
            if (this.gvDirecciones.Rows.Count > 0)
            {
                bool flag = false;
                Contactos_Direcciones obj3 = null;
                int num4 = this.gvDirecciones.Rows.Count - 1;
                for (int i = 0; i <= num4; i++)
                {
                    obj3 = new Contactos_Direcciones();
                    flag = false;
                    if (this.gvDirecciones.Rows[i].Cells["cdi_Direccion"].Value != null)
                    {
                        flag = true;
                        obj3.cdi_Direccion = this.gvDirecciones.Rows[i].Cells["cdi_Direccion"].Value.ToString();
                    }
                    if (this.gvDirecciones.Rows[i].Cells["cdi_Localidad"].Value != null)
                    {
                        flag = true;
                        obj3.cdi_Localidad = this.gvDirecciones.Rows[i].Cells["cdi_Localidad"].Value.ToString();
                    }
                    if (this.gvDirecciones.Rows[i].Cells["cdi_CodPostal"].Value != null)
                    {
                        flag = true;
                        obj3.cdi_CodPostal = this.gvDirecciones.Rows[i].Cells["cdi_CodPostal"].Value.ToString();
                    }
                    if (this.gvDirecciones.Rows[i].Cells["cdi_Tel1"].Value != null)
                    {
                        flag = true;
                        obj3.cdi_Tel1 = this.gvDirecciones.Rows[i].Cells["cdi_Tel1"].Value.ToString();
                    }
                    if (this.gvDirecciones.Rows[i].Cells["cdi_Tel2"].Value != null)
                    {
                        flag = true;
                        obj3.cdi_Tel2 = this.gvDirecciones.Rows[i].Cells["cdi_Tel2"].Value.ToString();
                    }
                    if (this.gvDirecciones.Rows[i].Cells["cdi_Observacion"].Value != null)
                    {
                        flag = true;
                        obj3.cdi_Observacion = this.gvDirecciones.Rows[i].Cells["cdi_Observacion"].Value.ToString();
                    }
                    obj3.con_ID = row.con_ID;
                    if (flag)
                    {
                        try
                        {
                            Contactos_DireccionesMapper.Instance().Insert(obj3);
                        }
                        catch (Exception exception6)
                        {
                            ProjectData.SetProjectError(exception6);
                            Exception exception3 = exception6;
                            MessageBox.Show("No se pudo guardar la direcci\x00f3n. Reintente por favor...\r\nError: " + exception3.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            ProjectData.ClearProjectError();
                            return;
                            ProjectData.ClearProjectError();
                        }
                    }
                }
            }
            if (this.gvPersonas.Rows.Count > 0)
            {
                Contactos_Personas obj4 = null;
                bool flag2 = false;
                int num5 = this.gvPersonas.Rows.Count - 1;
                for (int j = 0; j <= num5; j++)
                {
                    obj4 = new Contactos_Personas();
                    flag2 = false;
                    if (this.gvPersonas.Rows[j].Cells["cpe_Nombres"].Value != null)
                    {
                        flag2 = true;
                        obj4.cpe_Nombres = this.gvPersonas.Rows[j].Cells["cpe_Nombres"].Value.ToString();
                    }
                    if (this.gvPersonas.Rows[j].Cells["cpe_Puesto"].Value != null)
                    {
                        flag2 = true;
                        obj4.cpe_Puesto = this.gvPersonas.Rows[j].Cells["cpe_Puesto"].Value.ToString();
                    }
                    if (this.gvPersonas.Rows[j].Cells["cpe_Email"].Value != null)
                    {
                        flag2 = true;
                        obj4.cpe_Email = this.gvPersonas.Rows[j].Cells["cpe_Email"].Value.ToString();
                    }
                    if (this.gvPersonas.Rows[j].Cells["cpe_Tel1"].Value != null)
                    {
                        flag2 = true;
                        obj4.cpe_Tel1 = this.gvPersonas.Rows[j].Cells["cpe_Tel1"].Value.ToString();
                    }
                    if (this.gvPersonas.Rows[j].Cells["cpe_Tel2"].Value != null)
                    {
                        flag2 = true;
                        obj4.cpe_Tel2 = this.gvPersonas.Rows[j].Cells["cpe_Tel2"].Value.ToString();
                    }
                    if (Information.IsDate((this.gvPersonas.Rows[j].Cells["cpe_FechaNac"].Value)))
                    {
                        flag2 = true;
                        obj4.cpe_FechaNac = Conversions.ToDate(this.gvPersonas.Rows[j].Cells["cpe_FechaNac"].Value);
                    }
                    obj4.con_ID = row.con_ID;
                    if (flag2)
                    {
                        try
                        {
                            Contactos_PersonasMapper.Instance().Insert(obj4);
                        }
                        catch (Exception exception7)
                        {
                            ProjectData.SetProjectError(exception7);
                            Exception exception4 = exception7;
                            MessageBox.Show("No se pudo guardar la direcci\x00f3n. Reintente por favor...\r\nError: " + exception4.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            ProjectData.ClearProjectError();
                            return;
                            ProjectData.ClearProjectError();
                        }
                    }
                }
            }
            MessageBox.Show("El contacto se guard\x00f3 correctamente", "OK", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void Eliminar_Contacto()
        {
            try
            {
                ContactosMapper.Instance().Delete(this._contactos[this._indexContacto].con_ID);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudo eliminar el contacto. Aseg\x00farese de que el cliente no haya sido usado", "Eliminar cliente", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                ProjectData.ClearProjectError();
                return;
            }
            this.Traer_Contactos();
            this.BindingSource1.MoveFirst();
            this.cmdModificar.Enabled = false;
            this.cmdEliminar.Enabled = false;
            this.cmdPasarCli.Enabled = false;
            this._indexContacto = 0;
            if ((this._contactos != null) && (this._contactos.Count > 0))
            {
                this.Cargar_Contacto();
                this.cmdModificar.Enabled = true;
                this.cmdEliminar.Enabled = true;
                this.cmdPasarCli.Enabled = true;
            }
        }

        private void frmContactos_Load(object sender, EventArgs e)
        {
            this.Traer_Contactos();
            this.cmdGuardar.Enabled = false;
            this.cmdModificar.Enabled = true;
            this.cmdNuevo.Enabled = true;
            this.cmdEliminar.Enabled = false;
            if ((this._contactos != null) && (this._contactos.Count > 0))
            {
                this.Cargar_Contacto();
                this.cmdEliminar.Enabled = true;
            }
            this.Habilitar_Pantalla(false);
            this.cboCampos.SelectedIndex = 1;
        }

        private void Guardar_Contacto()
        {
            if (this._esModificacion)
            {
                this.Modificar_Contacto();
            }
            else
            {
                this.Crear_Contacto();
                this.Traer_Contactos();
                this._indexContacto = this._contactos.Count - 1;
                this.BindingSource1.MoveLast();
            }
        }

        private void Habilitar_Pantalla(bool habilitar)
        {
            this.PanEmpresa.Enabled = habilitar;
            this.gvPersonas.ReadOnly = !habilitar;
            this.gvDirecciones.ReadOnly = !habilitar;
            this.txtHistorial.Enabled = habilitar;
            this.panBotonera.Enabled = habilitar;
        }

        private void Limpiar_Contacto()
        {
            this.txtEmpresa.Text = string.Empty;
            this.txtEmail.Text = string.Empty;
            this.txtHistorial.Text = string.Empty;
            this.txtSitio.Text = string.Empty;
            this.txtCodCont.Text = string.Empty;
            this.cboCategorias.Text = string.Empty;
            this.txtActividad.Text = string.Empty;
            this.chkAviso.Checked = false;
            this.txtEmpresa.Focus();
            this._direcciones = null;
            this._personas = null;
            this.gvDirecciones.DataSource = this._direcciones;
            this.gvPersonas.DataSource = this._personas;
        }

        private void Modificar_Contacto()
        {
            Contactos row = new Contactos();
            row.con_ID = this._contactos[this._indexContacto].con_ID;
            row.con_Actividad = this.txtActividad.Text;
            if (this.cboCategorias.SelectedIndex > -1)
            {
                row.con_Categoria = (byte)(this.cboCategorias.SelectedIndex + 1);
            }
            if (this.txtEmail.Text.Trim() != string.Empty)
            {
                row.con_Email = this.txtEmail.Text.Trim();
            }
            if (this.txtHistorial.Text.Trim() != string.Empty)
            {
                row.con_Historial = this.txtHistorial.Text.Trim();
            }
            if ((this.txtCodCont.Text.Trim() != string.Empty) && Versioned.IsNumeric(this.txtCodCont.Text))
            {
                row.con_CodCont = Conversions.ToShort(this.txtCodCont.Text);
            }
            row.con_Nombre = this.txtEmpresa.Text.Trim();
            row.con_Aviso = this.chkAviso.Checked;
            if (this.txtSitio.Text.Trim() != string.Empty)
            {
                row.con_SitioWeb = this.txtSitio.Text.Trim();
            }
            row.con_FechaHora = DateAndTime.Now;
            try
            {
                ContactosMapper.Instance().Save(row);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudo guardar el contacto. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                ProjectData.ClearProjectError();
                return;
                ProjectData.ClearProjectError();
            }
            try
            {
                StreamWriter writer = new StreamWriter(System.Configuration.ConfigurationSettings.AppSettings["textosCon"] + row.con_ID.ToString() + ".rtf");
                writer.Write(this.txtHistorial.Rtf);
                writer.Close();
                this._cambio = true;
                this.chkItalic.Checked = false;
                this.chkNegrita.Checked = false;
                this.chkSubrayado.Checked = false;
                this.chkTachado.Checked = false;
            }
            catch (Exception exception6)
            {
                ProjectData.SetProjectError(exception6);
                Exception exception2 = exception6;
                ProjectData.ClearProjectError();
            }
            try
            {
                ContactosMapper.Instance().Save(row);
                Contactos_DireccionesMapper.Instance().DeleteByContactos(row.con_ID);
                Contactos_PersonasMapper.Instance().DeleteByContactos(row.con_ID);
            }
            catch (Exception exception7)
            {
                ProjectData.SetProjectError(exception7);
                Exception exception3 = exception7;
                MessageBox.Show("No se pudo actualizar el cliente seleccionado. Reintente por favor...\r\nError: " + exception3.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                ProjectData.ClearProjectError();
                return;
                ProjectData.ClearProjectError();
            }
            if (this._direcciones != null)
            {
                int num4 = this._direcciones.Count - 1;
                for (int i = 0; i <= num4; i++)
                {
                    this._direcciones[i].con_ID = row.con_ID;
                    try
                    {
                        Contactos_DireccionesMapper.Instance().Insert(this._direcciones[i]);
                    }
                    catch (Exception exception8)
                    {
                        ProjectData.SetProjectError(exception8);
                        Exception exception4 = exception8;
                        MessageBox.Show("No se pudo guardar la direcci\x00f3n. Reintente por favor...\r\nError: " + exception4.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        ProjectData.ClearProjectError();
                        return;
                        ProjectData.ClearProjectError();
                    }
                }
            }
            if (this._personas != null)
            {
                int num5 = this._personas.Count - 1;
                for (int j = 0; j <= num5; j++)
                {
                    this._personas[j].con_ID = row.con_ID;
                    try
                    {
                        Contactos_PersonasMapper.Instance().Insert(this._personas[j]);
                    }
                    catch (Exception exception9)
                    {
                        ProjectData.SetProjectError(exception9);
                        Exception exception5 = exception9;
                        MessageBox.Show("No se pudo guardar la persona. Reintente por favor...\r\nError: " + exception5.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        ProjectData.ClearProjectError();
                        return;
                        ProjectData.ClearProjectError();
                    }
                }
            }
            MessageBox.Show("El contacto se guard\x00f3 correctamente", "OK", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void Modificar_Texto()
        {
            if (this.chkNegrita.Checked)
            {
                if (this.chkSubrayado.Checked)
                {
                    if (this.chkTachado.Checked)
                    {
                        if (this.chkItalic.Checked)
                        {
                            this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic | FontStyle.Bold);
                        }
                        else
                        {
                            this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout | FontStyle.Underline | FontStyle.Bold);
                        }
                    }
                    else if (this.chkItalic.Checked)
                    {
                        this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Underline | FontStyle.Italic | FontStyle.Bold);
                    }
                    else
                    {
                        this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Underline | FontStyle.Bold);
                    }
                }
                else if (this.chkTachado.Checked)
                {
                    if (this.chkItalic.Checked)
                    {
                        this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout | FontStyle.Italic | FontStyle.Bold);
                    }
                    else
                    {
                        this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout | FontStyle.Bold);
                    }
                }
                else if (this.chkItalic.Checked)
                {
                    this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Italic | FontStyle.Bold);
                }
                else
                {
                    this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Bold);
                }
            }
            else if (this.chkSubrayado.Checked)
            {
                if (this.chkTachado.Checked)
                {
                    if (this.chkItalic.Checked)
                    {
                        this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic);
                    }
                    else
                    {
                        this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout | FontStyle.Underline);
                    }
                }
                else if (this.chkItalic.Checked)
                {
                    this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Underline | FontStyle.Italic);
                }
                else
                {
                    this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Underline);
                }
            }
            else if (this.chkTachado.Checked)
            {
                if (this.chkItalic.Checked)
                {
                    this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout | FontStyle.Italic);
                }
                else
                {
                    this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Strikeout);
                }
            }
            else if (this.chkItalic.Checked)
            {
                this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Italic);
            }
            else
            {
                this.txtHistorial.SelectionFont = new Font(this.txtHistorial.SelectionFont, FontStyle.Regular);
            }
        }

        private bool Pasar_A_Cliente()
        {
            int num;
            bool flag;
            Clientes row = new Clientes();
            Clientes_DireccionesList list = new Clientes_DireccionesList();
            Clientes_PersonasList list2 = new Clientes_PersonasList();
            Clientes_Direcciones item = null;
            Clientes_Personas obj4 = null;
            row.cli_Actividad = this.txtActividad.Text;
            if (this.cboCategorias.SelectedIndex > -1)
            {
                row.cli_Categoria = (byte)(this.cboCategorias.SelectedIndex + 1);
            }
            if (this.txtEmail.Text.Trim() != string.Empty)
            {
                row.cli_Email = this.txtEmail.Text.Trim();
            }
            if (this.txtHistorial.Text.Trim() != string.Empty)
            {
                row.cli_Historial = this.txtHistorial.Text.Trim();
            }
            row.cli_Nombre = this.txtEmpresa.Text.Trim();
            if (this.txtSitio.Text.Trim() != string.Empty)
            {
                row.cli_SitioWeb = this.txtSitio.Text.Trim();
            }
            row.cli_FechaHora = DateAndTime.Now;
            try
            {
                ClientesMapper.Instance().Insert(row);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se guardar el cliente. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
            }
            try
            {
                StreamWriter writer = new StreamWriter(ConfigurationManager.AppSettings["textosCli"] + row.cli_ID.ToString() + ".rtf");
                writer.Write(this.txtHistorial.Rtf);
                writer.Close();
                this._cambio = true;
                this.chkItalic.Checked = false;
                this.chkNegrita.Checked = false;
                this.chkSubrayado.Checked = false;
                this.chkTachado.Checked = false;
            }
            catch (Exception exception5)
            {
                ProjectData.SetProjectError(exception5);
                Exception exception2 = exception5;
                ProjectData.ClearProjectError();
            }
            if (this._direcciones != null)
            {
                int num3 = this._direcciones.Count - 1;
                for (num = 0; num <= num3; num++)
                {
                    item = new Clientes_Direcciones();
                    item.cdi_CodPostal = this._direcciones[num].cdi_CodPostal;
                    item.cdi_Direccion = this._direcciones[num].cdi_Direccion;
                    item.cdi_Localidad = this._direcciones[num].cdi_Localidad;
                    item.cdi_Observacion = this._direcciones[num].cdi_Observacion;
                    item.cdi_Tel1 = this._direcciones[num].cdi_Tel1;
                    item.cdi_Tel2 = this._direcciones[num].cdi_Tel2;
                    item.cli_ID = row.cli_ID;
                    list.Add(item);
                }
            }
            if (this._personas != null)
            {
                int num4 = this._personas.Count - 1;
                for (num = 0; num <= num4; num++)
                {
                    obj4 = new Clientes_Personas();
                    obj4.cpe_Apellido = this._personas[num].cpe_Apellido;
                    obj4.cpe_Email = this._personas[num].cpe_Email;
                    obj4.cpe_FechaNac = this._personas[num].cpe_FechaNac;
                    obj4.cpe_Nombres = this._personas[num].cpe_Nombres;
                    obj4.cpe_Puesto = this._personas[num].cpe_Puesto;
                    obj4.cpe_Tel1 = this._personas[num].cpe_Tel1;
                    obj4.cpe_Tel2 = this._personas[num].cpe_Tel2;
                    obj4.cli_ID = row.cli_ID;
                    list2.Add(obj4);
                }
            }
            if (list.Count > 0)
            {
                int num5 = list.Count - 1;
                for (num = 0; num <= num5; num++)
                {
                    try
                    {
                        Clientes_DireccionesMapper.Instance().Insert(list[num]);
                    }
                    catch (Exception exception6)
                    {
                        ProjectData.SetProjectError(exception6);
                        Exception exception3 = exception6;
                        MessageBox.Show("No se pudo guardar la direcci\x00f3n. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        flag = false;
                        ProjectData.ClearProjectError();
                        return flag;
                        ProjectData.ClearProjectError();
                    }
                }
            }
            if (list2.Count > 0)
            {
                int num6 = list2.Count - 1;
                for (num = 0; num <= num6; num++)
                {
                    try
                    {
                        Clientes_PersonasMapper.Instance().Insert(list2[num]);
                    }
                    catch (Exception exception7)
                    {
                        ProjectData.SetProjectError(exception7);
                        Exception exception4 = exception7;
                        MessageBox.Show("No se pudo guardar la persona. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        flag = false;
                        ProjectData.ClearProjectError();
                        return flag;
                        ProjectData.ClearProjectError();
                    }
                }
            }
            return true;
        }

        private void Traer_Contactos()
        {
            try
            {
                this._contactos = ContactosMapper.Instance().GetAll();
                this.BindingSource1.DataSource = this._contactos;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer los contactos. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this._contactos = null;
                ProjectData.ClearProjectError();
            }
        }

        private void Traer_Direcciones(int conID)
        {
            try
            {
                this._direcciones = Contactos_DireccionesMapper.Instance().GetByContactos(conID);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer las direcciones del contacto seleccionado. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                ProjectData.ClearProjectError();
                return;
                ProjectData.ClearProjectError();
            }
            this.ContactosDireccionesObjectBindingSource.DataSource = null;
            this.ContactosDireccionesObjectBindingSource.DataSource = this._direcciones;
            this.gvDirecciones.DataSource = this.ContactosDireccionesObjectBindingSource;
        }

        private void Traer_Personas(int conID)
        {
            try
            {
                this._personas = Contactos_PersonasMapper.Instance().GetByContactos(conID);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer las personas del contacto seleccionado. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                ProjectData.ClearProjectError();
                return;
                ProjectData.ClearProjectError();
            }
            this.ContactosPersonasObjectBindingSource.DataSource = null;
            this.ContactosPersonasObjectBindingSource.DataSource = this._personas;
            this.gvPersonas.DataSource = this.ContactosPersonasObjectBindingSource;
        }

        private void Traer_X_Campo()
        {
            string str;
            byte num=0;
            switch (this.cboCampos.SelectedIndex)
            {
                case 0:
                    str = "todos";
                    num = 4;
                    break;

                case 1:
                    str = "con_Nombre";
                    num = 1;
                    break;

                case 2:
                    str = "con_CodCont";
                    num = 1;
                    break;

                case 3:
                    str = "con_Email";
                    num = 1;
                    break;

                case 4:
                    str = "con_SitioWeb";
                    num = 1;
                    break;

                case 5:
                    str = "con_Historial";
                    num = 1;
                    break;

                case 6:
                    str = "cdi_Direccion";
                    num = 2;
                    break;

                case 7:
                    str = "cdi_Localidad";
                    num = 2;
                    break;

                case 8:
                    str = "cdi_Tel1";
                    num = 2;
                    break;

                case 9:
                    str = "cdi_Tel2";
                    num = 2;
                    break;

                case 10:
                    str = "cdi_CodPostal";
                    num = 2;
                    break;

                case 11:
                    str = "cdi_Observacion";
                    num = 2;
                    break;

                case 12:
                    str = "cpe_Nombres";
                    num = 3;
                    break;

                case 13:
                    str = "cpe_Apellido";
                    num = 3;
                    break;

                case 14:
                    str = "cpe_Email";
                    num = 3;
                    break;

                case 15:
                    str = "cpe_Puesto";
                    num = 3;
                    break;

                case 0x10:
                    str = "cpe_Tel1";
                    num = 3;
                    break;

                case 0x11:
                    str = "cpe_Tel2";
                    num = 3;
                    break;

                default:
                    str = "";
                    break;
            }
            try
            {
                switch (((byte)(num - 1)))
                {
                    case 0:
                        this._contactos = ContactosMapper.Instance().Traer_X_Campo(str, this.txtTexto.Text.Trim());
                        break;

                    case 1:
                        this._contactos = ContactosMapper.Instance().Traer_X_Campo_Direccion(str, this.txtTexto.Text.Trim());
                        break;

                    case 2:
                        this._contactos = ContactosMapper.Instance().Traer_X_Campo_Persona(str, this.txtTexto.Text.Trim());
                        break;

                    case 3:
                        this._contactos = ContactosMapper.Instance().Traer_X_Campo_Todos(this.txtTexto.Text.Trim());
                        break;
                }
                this.BindingSource1.DataSource = this._contactos;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer los contactos. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this._contactos = null;
                ProjectData.ClearProjectError();
            }
        }

        private void Traer_X_Categoria()
        {
            try
            {
                this._contactos = ContactosMapper.Instance().Traer_X_Categoria((byte)(this.cboCategorias2.SelectedIndex + 1));
                this.BindingSource1.DataSource = this._contactos;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer los contactos. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this._contactos = null;
                ProjectData.ClearProjectError();
            }
        }

        private void txtCodCont_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                this.cboCategorias.Focus();
            }
        }

        private void txtEmail_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                this.txtCodCont.Focus();
            }
        }

        private void txtEmpresa_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                this.txtActividad.Focus();
            }
        }

        private void txtHistorial_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((((int)e.KeyChar == 13) && (1 == this.txtHistorial.GetLineFromCharIndex(this.txtHistorial.GetFirstCharIndexOfCurrentLine()))) && (this.txtHistorial.GetFirstCharIndexOfCurrentLine() == 1))
            {
                this.txtHistorial.Rtf = this.txtHistorial.Rtf.Insert(0x77, " " + DateAndTime.Now.ToString("dd/MM/yyyy  HH:mm") + ":");
                this.txtHistorial.Select(0x12, 0);
            }
        }

        private void txtHistorial_SelectionChanged(object sender, EventArgs e)
        {
            this._cambio = true;
            if (this.txtHistorial.SelectionFont != null)
            {
                this.chkNegrita.Checked = this.txtHistorial.SelectionFont.Bold;
            }
            if (this.txtHistorial.SelectionFont != null)
            {
                this.chkSubrayado.Checked = this.txtHistorial.SelectionFont.Underline;
            }
            if (this.txtHistorial.SelectionFont != null)
            {
                this.chkTachado.Checked = this.txtHistorial.SelectionFont.Strikeout;
            }
            if (this.txtHistorial.SelectionFont != null)
            {
                this.chkItalic.Checked = this.txtHistorial.SelectionFont.Italic;
            }
            this._cambio = false;
        }

        private void txtSitio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                this.txtHistorial.Focus();
            }
        }

        private void txtTexto_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                this.cboBuscar2.PerformClick();
            }
        }

        private bool Validar_Contacto()
        {
            if (this.txtEmpresa.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Debe ingresar un nombre v\x00e1lido para el contacto", "Campo vac\x00edo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            return true;
        }


    }
}